<template>
  <div class="chat-container">
    <!-- 动态背景 -->
    <div class="love-background">
      <div class="gradient-overlay"></div>
      <div class="floating-hearts">
        <div v-for="n in 30" :key="n" class="heart"></div>
      </div>
      <div class="sparkles">
        <div v-for="n in 50" :key="n" class="sparkle"></div>
      </div>
    </div>
    
    <!-- 聊天头部 -->
    <div class="chat-header">
      <el-button @click="$router.push('/')" type="text" class="back-btn">
        <el-icon><ArrowLeft /></el-icon> 返回首页
      </el-button>
      <div class="header-content">
        <div class="title-section">
          <div class="icon-wrapper">
                         <el-icon class="love-icon"><Star /></el-icon>
          </div>
          <div class="title-text">
            <h2>AI 恋爱大师</h2>
            <p class="subtitle">你的专属情感顾问</p>
          </div>
        </div>
        <div class="chat-info">
          <el-icon><Connection /></el-icon>
          <span>会话ID: {{ chatId }}</span>
        </div>
      </div>
    </div>
    
    <!-- 消息区域 -->
    <div class="chat-messages" ref="messagesContainer">
      <div v-for="(msg, index) in messages" :key="index" 
           :class="['message', msg.type === 'user' ? 'message-user' : 'message-ai']">
        <div class="message-avatar">
          <el-icon v-if="msg.type === 'ai'"><Service /></el-icon>
          <el-icon v-else><User /></el-icon>
        </div>
        <div class="message-bubble">
          <div class="message-content" v-html="renderMarkdown(msg.content)"></div>
          <div class="message-time">{{ formatTime(msg.timestamp) }}</div>
        </div>
      </div>
    </div>

    <!-- 输入区域 -->
    <div class="chat-input">
      <div class="input-wrapper">
        <el-input
          v-model="inputMessage"
          type="textarea"
          :rows="3"
          placeholder="💕 告诉我你的恋爱困惑，我会用心为你解答..."
          @keyup.enter.ctrl="sendMessage"
          class="love-input"
        />
        <div class="input-actions">
          <el-button 
            type="primary" 
            @click="sendMessage" 
            :loading="loading" 
            class="send-btn"
            :disabled="!inputMessage.trim()"
          >
            <el-icon><Promotion /></el-icon>
            发送
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { v4 as uuidv4 } from 'uuid'
import { createLoveMasterChat } from '../api'
import { ArrowLeft, ChatDotRound, Connection, Service, User, Promotion, Star } from '@element-plus/icons-vue'

const chatId = ref(uuidv4())
const messages = ref([])
const inputMessage = ref('')
const loading = ref(false)
const messagesContainer = ref(null)

const scrollToBottom = async () => {
  await nextTick()
  if (messagesContainer.value) {
    messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
  }
}

const formatTime = (timestamp) => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  return date.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit' 
  })
}

// 简单的Markdown渲染函数
const renderMarkdown = (content) => {
  if (!content) return ''
  
  return content
    // 处理图片 ![alt](url)
    .replace(/!\[([^\]]*)\]\(([^)]+)\)/g, '<img src="$2" alt="$1" class="chat-image" />')
    // 处理粗体 **text**
    .replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
    // 处理换行
    .replace(/\n/g, '<br>')
    // 处理列表
    .replace(/^(\d+)\.\s+/gm, '<span class="list-number">$1.</span> ')
}

const sendMessage = async () => {
  if (!inputMessage.value.trim() || loading.value) return

  const message = inputMessage.value
  messages.value.push({ 
    type: 'user', 
    content: message, 
    timestamp: new Date().toISOString() 
  })
  inputMessage.value = ''
  loading.value = true
  
  await scrollToBottom()

  const eventSource = createLoveMasterChat(message, chatId.value)
  
  let aiResponse = ''
  
  eventSource.onmessage = (event) => {
    const chunk = event.data
    if (aiResponse === '') {
      messages.value.push({ 
        type: 'ai', 
        content: chunk, 
        timestamp: new Date().toISOString() 
      })
    } else {
      messages.value[messages.value.length - 1].content += chunk
    }
    aiResponse += chunk
    scrollToBottom()
  }

  eventSource.onerror = () => {
    eventSource.close()
    loading.value = false
  }
}

onMounted(() => {
  // 可以在这里添加欢迎消息
  messages.value.push({ 
    type: 'ai', 
    content: '💕 你好！我是你的AI恋爱顾问，有什么恋爱问题需要咨询吗？我会用温暖的心为你解答每一个困惑～',
    timestamp: new Date().toISOString()
  })
})
</script>

<style scoped>
.chat-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.love-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #ff6b9d, #c44569, #f8b5d3);
  z-index: 0;
  overflow: hidden;
}

.gradient-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
              radial-gradient(circle at 70% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  z-index: 1;
}

.floating-hearts {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 2;
}

.sparkles {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 2;
}

.sparkle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  animation: sparkle 3s infinite;
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.6);
}

@keyframes sparkle {
  0%, 100% {
    opacity: 0;
    transform: scale(0);
  }
  50% {
    opacity: 1;
    transform: scale(1);
  }
}

.heart {
  position: absolute;
  width: 20px;
  height: 20px;
  background: rgba(255, 255, 255, 0.3);
  transform: rotate(45deg);
  animation: float 6s infinite linear;
}

.heart::before,
.heart::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 50%;
}

.heart::before {
  top: -10px;
  left: 0;
}

.heart::after {
  top: 0;
  left: -10px;
}

@keyframes float {
  0% {
    transform: rotate(45deg) translateY(100vh) scale(0);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: rotate(45deg) translateY(-100px) scale(1);
    opacity: 0;
  }
}

.chat-header {
  padding: 20px 24px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 20px;
  position: relative;
  z-index: 10;
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}

.back-btn {
  color: #c44569;
  font-weight: 500;
}

.header-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.icon-wrapper {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #ff6b9d, #c44569);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 15px rgba(255, 107, 157, 0.3);
}

.love-icon {
  font-size: 24px;
  color: #ffffff;
}

.title-text h2 {
  margin: 0;
  color: #c44569;
  font-size: 1.8rem;
  font-weight: 700;
  line-height: 1.2;
}

.subtitle {
  margin: 4px 0 0 0;
  color: #666;
  font-size: 0.9rem;
  font-weight: 400;
}



.chat-info {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666;
  font-size: 0.85rem;
  background: rgba(255, 107, 157, 0.08);
  padding: 8px 16px;
  border-radius: 25px;
  border: 1px solid rgba(255, 107, 157, 0.15);
  backdrop-filter: blur(10px);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 15px;
  position: relative;
  z-index: 1;
}

.message {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 20px;
  max-width: 80%;
}

.message-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 18px;
}

.message-ai .message-avatar {
  background: linear-gradient(135deg, #ff6b9d, #c44569);
  color: white;
  box-shadow: 0 4px 15px rgba(255, 107, 157, 0.3);
}

.message-user .message-avatar {
  background: linear-gradient(135deg, #409EFF, #67C23A);
  color: white;
  box-shadow: 0 4px 15px rgba(64, 158, 255, 0.3);
}

.message-bubble {
  flex: 1;
  min-width: 0;
}

.message-content {
  padding: 16px 20px;
  border-radius: 20px;
  word-break: break-word;
  position: relative;
  backdrop-filter: blur(10px);
  line-height: 1.6;
}

.message-ai {
  align-self: flex-start;
}

.message-ai .message-content {
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  color: #333;
}

.message-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-user .message-content {
  background: linear-gradient(135deg, #ff6b9d, #c44569);
  color: #fff;
  box-shadow: 0 4px 15px rgba(255, 107, 157, 0.3);
}

.message-time {
  font-size: 0.75rem;
  color: #999;
  margin-top: 6px;
  text-align: right;
}

.message-user .message-time {
  text-align: left;
}

.message-content :deep(.chat-image) {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  margin: 10px 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.message-content :deep(.list-number) {
  font-weight: bold;
  color: #ff6b9d;
  margin-right: 8px;
}

.message-content :deep(strong) {
  font-weight: bold;
}

.chat-input {
  padding: 24px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  position: relative;
  z-index: 10;
  border-top: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.1);
}

.input-wrapper {
  display: flex;
  gap: 16px;
  align-items: flex-end;
}

.input-actions {
  display: flex;
  align-items: center;
}

.love-input {
  flex: 1;
}

.love-input :deep(.el-textarea__inner) {
  resize: none;
  border-radius: 25px;
  border: 2px solid rgba(255, 107, 157, 0.2);
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
  padding: 16px 20px;
  font-size: 14px;
  line-height: 1.5;
}

.love-input :deep(.el-textarea__inner:focus) {
  border-color: #ff6b9d;
  box-shadow: 0 0 0 4px rgba(255, 107, 157, 0.15);
  background: rgba(255, 255, 255, 0.95);
}

.love-input :deep(.el-textarea__inner::placeholder) {
  color: rgba(255, 107, 157, 0.6);
  font-style: italic;
}

.send-btn {
  background: linear-gradient(135deg, #ff6b9d, #c44569);
  border: none;
  border-radius: 25px;
  padding: 16px 28px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  font-weight: 600;
  font-size: 14px;
  box-shadow: 0 4px 15px rgba(255, 107, 157, 0.3);
}

.send-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(255, 107, 157, 0.5);
}

.send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

/* 为浮动爱心设置随机位置和延迟 */
.heart:nth-child(1) { left: 10%; animation-delay: 0s; }
.heart:nth-child(2) { left: 20%; animation-delay: 1s; }
.heart:nth-child(3) { left: 30%; animation-delay: 2s; }
.heart:nth-child(4) { left: 40%; animation-delay: 3s; }
.heart:nth-child(5) { left: 50%; animation-delay: 4s; }
.heart:nth-child(6) { left: 60%; animation-delay: 5s; }
.heart:nth-child(7) { left: 70%; animation-delay: 0.5s; }
.heart:nth-child(8) { left: 80%; animation-delay: 1.5s; }
.heart:nth-child(9) { left: 90%; animation-delay: 2.5s; }
.heart:nth-child(10) { left: 15%; animation-delay: 3.5s; }
.heart:nth-child(11) { left: 25%; animation-delay: 4.5s; }
.heart:nth-child(12) { left: 35%; animation-delay: 5.5s; }
.heart:nth-child(13) { left: 45%; animation-delay: 1.2s; }
.heart:nth-child(14) { left: 55%; animation-delay: 2.2s; }
.heart:nth-child(15) { left: 65%; animation-delay: 3.2s; }
.heart:nth-child(16) { left: 75%; animation-delay: 4.2s; }
.heart:nth-child(17) { left: 85%; animation-delay: 5.2s; }
.heart:nth-child(18) { left: 95%; animation-delay: 0.8s; }
.heart:nth-child(19) { left: 5%; animation-delay: 1.8s; }
.heart:nth-child(20) { left: 85%; animation-delay: 2.8s; }
.heart:nth-child(21) { left: 12%; animation-delay: 3.8s; }
.heart:nth-child(22) { left: 22%; animation-delay: 4.8s; }
.heart:nth-child(23) { left: 32%; animation-delay: 5.8s; }
.heart:nth-child(24) { left: 42%; animation-delay: 1.5s; }
.heart:nth-child(25) { left: 52%; animation-delay: 2.5s; }
.heart:nth-child(26) { left: 62%; animation-delay: 3.5s; }
.heart:nth-child(27) { left: 72%; animation-delay: 4.5s; }
.heart:nth-child(28) { left: 82%; animation-delay: 5.5s; }
.heart:nth-child(29) { left: 92%; animation-delay: 0.3s; }
.heart:nth-child(30) { left: 8%; animation-delay: 1.3s; }

/* 为闪烁粒子设置随机位置和延迟 */
.sparkle:nth-child(1) { left: 5%; top: 10%; animation-delay: 0s; }
.sparkle:nth-child(2) { left: 15%; top: 20%; animation-delay: 0.5s; }
.sparkle:nth-child(3) { left: 25%; top: 15%; animation-delay: 1s; }
.sparkle:nth-child(4) { left: 35%; top: 25%; animation-delay: 1.5s; }
.sparkle:nth-child(5) { left: 45%; top: 10%; animation-delay: 2s; }
.sparkle:nth-child(6) { left: 55%; top: 30%; animation-delay: 2.5s; }
.sparkle:nth-child(7) { left: 65%; top: 20%; animation-delay: 3s; }
.sparkle:nth-child(8) { left: 75%; top: 15%; animation-delay: 3.5s; }
.sparkle:nth-child(9) { left: 85%; top: 25%; animation-delay: 4s; }
.sparkle:nth-child(10) { left: 95%; top: 10%; animation-delay: 4.5s; }
.sparkle:nth-child(11) { left: 10%; top: 40%; animation-delay: 5s; }
.sparkle:nth-child(12) { left: 20%; top: 35%; animation-delay: 5.5s; }
.sparkle:nth-child(13) { left: 30%; top: 45%; animation-delay: 6s; }
.sparkle:nth-child(14) { left: 40%; top: 50%; animation-delay: 6.5s; }
.sparkle:nth-child(15) { left: 50%; top: 40%; animation-delay: 7s; }
.sparkle:nth-child(16) { left: 60%; top: 55%; animation-delay: 7.5s; }
.sparkle:nth-child(17) { left: 70%; top: 45%; animation-delay: 8s; }
.sparkle:nth-child(18) { left: 80%; top: 50%; animation-delay: 8.5s; }
.sparkle:nth-child(19) { left: 90%; top: 40%; animation-delay: 9s; }
.sparkle:nth-child(20) { left: 5%; top: 60%; animation-delay: 9.5s; }
.sparkle:nth-child(21) { left: 15%; top: 65%; animation-delay: 10s; }
.sparkle:nth-child(22) { left: 25%; top: 70%; animation-delay: 10.5s; }
.sparkle:nth-child(23) { left: 35%; top: 75%; animation-delay: 11s; }
.sparkle:nth-child(24) { left: 45%; top: 80%; animation-delay: 11.5s; }
.sparkle:nth-child(25) { left: 55%; top: 85%; animation-delay: 12s; }
.sparkle:nth-child(26) { left: 65%; top: 90%; animation-delay: 12.5s; }
.sparkle:nth-child(27) { left: 75%; top: 85%; animation-delay: 13s; }
.sparkle:nth-child(28) { left: 85%; top: 80%; animation-delay: 13.5s; }
.sparkle:nth-child(29) { left: 95%; top: 75%; animation-delay: 14s; }
.sparkle:nth-child(30) { left: 10%; top: 90%; animation-delay: 14.5s; }
.sparkle:nth-child(31) { left: 20%; top: 95%; animation-delay: 15s; }
.sparkle:nth-child(32) { left: 30%; top: 88%; animation-delay: 15.5s; }
.sparkle:nth-child(33) { left: 40%; top: 92%; animation-delay: 16s; }
.sparkle:nth-child(34) { left: 50%; top: 87%; animation-delay: 16.5s; }
.sparkle:nth-child(35) { left: 60%; top: 93%; animation-delay: 17s; }
.sparkle:nth-child(36) { left: 70%; top: 89%; animation-delay: 17.5s; }
.sparkle:nth-child(37) { left: 80%; top: 94%; animation-delay: 18s; }
.sparkle:nth-child(38) { left: 90%; top: 91%; animation-delay: 18.5s; }
.sparkle:nth-child(39) { left: 5%; top: 85%; animation-delay: 19s; }
.sparkle:nth-child(40) { left: 15%; top: 88%; animation-delay: 19.5s; }
.sparkle:nth-child(41) { left: 25%; top: 92%; animation-delay: 20s; }
.sparkle:nth-child(42) { left: 35%; top: 87%; animation-delay: 20.5s; }
.sparkle:nth-child(43) { left: 45%; top: 90%; animation-delay: 21s; }
.sparkle:nth-child(44) { left: 55%; top: 94%; animation-delay: 21.5s; }
.sparkle:nth-child(45) { left: 65%; top: 88%; animation-delay: 22s; }
.sparkle:nth-child(46) { left: 75%; top: 93%; animation-delay: 22.5s; }
.sparkle:nth-child(47) { left: 85%; top: 89%; animation-delay: 23s; }
.sparkle:nth-child(48) { left: 95%; top: 86%; animation-delay: 23.5s; }
.sparkle:nth-child(49) { left: 8%; top: 82%; animation-delay: 24s; }
.sparkle:nth-child(50) { left: 18%; top: 78%; animation-delay: 24.5s; }
</style>